class MenuItemsManager
	include GetText
	GetText.bindtextdomain("SmarTask", "locale", nil, "utf-8")
  def initialize uicontext
    initCreateTaskMenuItem uicontext
    initCreateSubTaskMenuItem uicontext
    initUpdateTaskMenuItem uicontext
    initDeleteTaskMenuIteam uicontext
    
    initFileSaveMenuItem uicontext
    initFileSaveAsMenuItem uicontext
    initFileOpenMenuItem uicontext
    
    initHelpAboutMenuItem
		initChineseMenuItem
		initEnglishMenuItem
  end
  
  def initCreateTaskMenuItem uicontext
    @createTaskMenuItem = Gtk::MenuItem.new GLib.locale_to_utf8(_("_Create Task"))
    @createTaskMenuItem.add_events(Gdk::Event::BUTTON_PRESS_MASK)
    @createTaskMenuItem.signal_connect("button_press_event") do |widget,event|
      if event.button == 1
        form = TaskForm.new nil
        dialog = Gtk::Dialog.new(GLib.locale_to_utf8(_("Create Task")),$main_application_window,Gtk::Dialog::DESTROY_WITH_PARENT,[ Gtk::Stock::OK, Gtk::Dialog::RESPONSE_ACCEPT],[Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
        dialog.signal_connect :response do |dialog, response|
          if response == Gtk::Dialog::RESPONSE_ACCEPT
            #uicontext.listPanelManager.addTask2Panel form.getCurrentTask
            uicontext.treePanelManager.addTask2Panel form.getCurrentTask,nil
            uicontext.context.taskManager.addTask form.getCurrentTask
          end
          dialog.destroy
        end
        dialog.vbox.add form.getForm
        dialog.show_all
      end
    end
  end
  
  def getCreateTaskMenuItem
    @createTaskMenuItem
  end
  
  def initCreateSubTaskMenuItem uicontext
    @createSubTaskMenuItem = Gtk::MenuItem.new GLib.locale_to_utf8(_("_Create Sub Task"))
    @createSubTaskMenuItem.add_events(Gdk::Event::BUTTON_PRESS_MASK)
    @createSubTaskMenuItem.signal_connect("button_press_event") do |widget,event|
      if event.button == 1
        form = TaskForm.new nil
        dialog = Gtk::Dialog.new(GLib.locale_to_utf8(_("Create Task")),$main_application_window,Gtk::Dialog::DESTROY_WITH_PARENT,[ Gtk::Stock::OK, Gtk::Dialog::RESPONSE_ACCEPT],[Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
        dialog.signal_connect :response do |dialog, response|
          if response == Gtk::Dialog::RESPONSE_ACCEPT
            #uicontext.listPanelManager.addTask2Panel form.getCurrentTask
            uicontext.treePanelManager.addSubTask2Panel form.getCurrentTask
            uicontext.context.taskManager.addTask form.getCurrentTask
          end
          dialog.destroy
        end
        dialog.vbox.add form.getForm
        dialog.show_all
      end
    end
  end
  
  def getCreateSubTaskMenuItem
    @createSubTaskMenuItem
  end
  
  def initUpdateTaskMenuItem uicontext
    @updateTaskMenuItem = Gtk::MenuItem.new GLib.locale_to_utf8(_("_Update Task"))
    @updateTaskMenuItem.add_events(Gdk::Event::BUTTON_PRESS_MASK)
    @updateTaskMenuItem.signal_connect("button_press_event") do |widget,event|
      form = nil
      if event.button == 1
        if uicontext.selected != nil
          form = TaskForm.new(uicontext.context.taskManager.getTaskById(uicontext.selected[TreePanelManager::COLUMN_ID]))
          
          dialog = Gtk::Dialog.new(GLib.locale_to_utf8(_("Update Task")),$main_application_window,Gtk::Dialog::DESTROY_WITH_PARENT,[ Gtk::Stock::OK, Gtk::Dialog::RESPONSE_ACCEPT],[Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
          dialog.signal_connect :response do |dialog, response|
            if response == Gtk::Dialog::RESPONSE_ACCEPT
              #uicontext.listPanelManager.refreshTask2Panel form.getCurrentTask
              uicontext.treePanelManager.refreshTask2Panel form.getCurrentTask
            end
            dialog.destroy
          end
          dialog.vbox.add form.getForm
          dialog.show_all
        end
      end
    end
  end
  
  def getUpdateTaskMenuItem
    @updateTaskMenuItem
  end
  
  def initDeleteTaskMenuIteam uicontext
    @deleteTaskMenuItem = Gtk::MenuItem.new GLib.locale_to_utf8(_("_Delete Task"))
    @deleteTaskMenuItem.add_events(Gdk::Event::BUTTON_PRESS_MASK)
    @deleteTaskMenuItem.signal_connect("button_press_event") do |widget,event|
      if uicontext.selected != nil
        if event.button == 1
          #uicontext.context.taskManager.deleteTaskById uicontext.listPanelManager.getSelectedTaskId
          #uicontext.listPanelManager.removeTask
					uicontext.context.taskManager.deleteTaskAndChildrenById uicontext.selected[TreePanelManager::COLUMN_ID]
          uicontext.treePanelManager.removeTask
        end
      end
    end
  end
  
  def getDeleteTaskMenuItem
    @deleteTaskMenuItem
  end
  
  def initFileOpenMenuItem uicontext
    @fileOpenMenuItem = Gtk::MenuItem.new GLib.locale_to_utf8(_("_Open"))
    @fileOpenMenuItem.signal_connect("activate") {
      fileDialog = FileHandleDialog.new nil,GLib.locale_to_utf8(_("Open File")),Gtk::FileChooser::ACTION_OPEN,nil
      openPath = nil
      fileDialog.run do |response|
        openPath = fileDialog.filename if response == Gtk::Dialog::RESPONSE_OK
      end
      if openPath != nil
        TaskXMLParser.new.getTasksFromFile openPath,uicontext.context.taskManager.getTasks
        #uicontext.listPanelManager.refreshPanel uicontext.context.taskManager.getTasks
        uicontext.treePanelManager.refreshPanel uicontext.context.taskManager.getTasks
        uicontext.context.currentFilePath = openPath
      end
      fileDialog.destroy
    }    
  end
  
  def getFileOpenMenuItem
    @fileOpenMenuItem
  end
  
  def initFileSaveMenuItem uicontext
    @fileSaveMenuItem = Gtk::MenuItem.new GLib.locale_to_utf8(_("_Save"))
    @fileSaveMenuItem.signal_connect("activate") {
      savePath = uicontext.context.currentFilePath
      if savePath == nil
        fileDialog = FileHandleDialog.new nil,GLib.locale_to_utf8(_("Save")),Gtk::FileChooser::ACTION_SAVE,"my_task.xml"
        savePath = nil
        fileDialog.run do |response|
          savePath = fileDialog.filename if response == Gtk::Dialog::RESPONSE_OK
					uicontext.context.currentFilePath = savePath
        end
        fileDialog.destroy
      end
      TaskXMLBuilder.new.write2XML uicontext.context.taskManager.getTasks,savePath
    }    
  end
  
  def getFileSaveMenuItem
    @fileSaveMenuItem
  end
  
  def initFileSaveAsMenuItem uicontext
    @fileSaveAsMenuItem = Gtk::MenuItem.new GLib.locale_to_utf8(_("_SaveAs"))
    @fileSaveAsMenuItem.signal_connect("activate") {
      fileDialog = FileHandleDialog.new nil,GLib.locale_to_utf8(_("Save As")),Gtk::FileChooser::ACTION_SAVE,"my_task.xml"
      savePath = nil
      fileDialog.run do |response|
        savePath = fileDialog.filename if response == Gtk::Dialog::RESPONSE_OK
      end
      if savePath != nil
        TaskXMLBuilder.new.write2XML uicontext.context.taskManager.getTasks,savePath
      else
        
      end
      fileDialog.destroy
    }
  end
  
  def getFileSaveAsMenuItem
    @fileSaveAsMenuItem  
  end
  
  def initHelpAboutMenuItem
    @helpAboutMenuItem = Gtk::MenuItem.new GLib.locale_to_utf8(_("_About"))
    @helpAboutMenuItem.signal_connect("activate") {
      Gtk::AboutDialog.show(nil,
        "authors" => ["Jomper.Chow Jomper.cn@gmail.com 
        								\nThanks:
        								\n李斌 Ben.upsilon ben.upsilon@gmail.com
        								\nSemovy semovy@gmail.com
        								\n黄顺龙 huangshunlong@live.cn
        								\n张浩翔 blackanger.z@gmail.com
												\n勾雪"],
        "comments" => "Ruby Task Manager",
        "copyright" => "Copyright (C) 2008 SmarTask",
        "license" => "Apache License 2.0",
        "logo_icon_name" => "gtk-home",
        "version" => "0.3",
        "website" => "http://code.google.com/p/smartask/",
        "website_label" => "SmarTask Project Website",
        "name" => "SmarTask"
      )
    }
  end
  
  def getHelpAboutMenuItem
    @helpAboutMenuItem
  end
  
  def initChineseMenuItem
		@chineseMenuItem = Gtk::RadioMenuItem.new GLib.locale_to_utf8(_("_Chinese")),false
		#@chineseMenuItem.select if "zh".eql?(TaskXMLParser.new.getConfigure(MainUI::CONFIG_PATH))
		@group = @chineseMenuItem.group
		@chineseMenuItem.signal_connect("button_press_event") {|widget,event|
			if event.button == 1
				TaskXMLBuilder.new.changeLanguage "zh",MainUI::CONFIG_PATH
				dialog = Gtk::Dialog.new(GLib.locale_to_utf8(_("Prompt")),$main_application_window,Gtk::Dialog::DESTROY_WITH_PARENT,[ Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK])
	      dialog.signal_connect :response do |_dialog, response|
	        _dialog.destroy
	      end
	      dialog.vbox.add Gtk::Label.new(GLib.locale_to_utf8(("_Need to restart software :)")), true)
	      dialog.show_all
			end
		}
  end
  
  def getChineseMenuItem
		@chineseMenuItem
  end
  
	def initEnglishMenuItem
		@englishMenuItem = Gtk::RadioMenuItem.new @group,GLib.locale_to_utf8(_("_English")),false
		#@englishMenuItem.select if "en".eql?(TaskXMLParser.new.getConfigure(MainUI::CONFIG_PATH))
		@englishMenuItem.signal_connect("button_press_event") {|widget,event|
			if event.button == 1
				TaskXMLBuilder.new.changeLanguage "en",MainUI::CONFIG_PATH
				dialog = Gtk::Dialog.new(GLib.locale_to_utf8(_("Prompt")),$main_application_window,Gtk::Dialog::DESTROY_WITH_PARENT,[ Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK])
	      dialog.signal_connect :response do |_dialog, response|
	        _dialog.destroy
	      end
	      dialog.vbox.add Gtk::Label.new(GLib.locale_to_utf8(_("Need to restart software :)")), true)
	      dialog.show_all
		  end
		}
	end
	
	def getEnglishMenuItem
		@englishMenuItem
	end
end